package com.example.order.controller;

import com.example.common.enums.ResultCode;
import com.example.common.req.payment.PaymentReq;
import com.example.common.resp.GlobalResult;
import com.example.common.resp.payment.PaymentResp;
import com.example.order.remote.PaymentService;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Slf4j
@RestController
@RequestMapping("/order")
@DefaultProperties(defaultFallback = "fallback")  //全局的
public class OrderController {

    @Resource
    private PaymentService paymentService;

    @PostMapping(value = "/payment/create")
    public GlobalResult<Long> create(@RequestBody @Validated PaymentReq req) {
        return paymentService.create(req);
    }

    @GetMapping(value = "/payment/get/{id}")
    public GlobalResult<PaymentResp> get(@PathVariable("id") Long id) {
        return paymentService.get(id);
    }

    @GetMapping(value = "/payment/success")
    public GlobalResult<String> success() {
        return paymentService.success();
    }

    @GetMapping(value = "/payment/timeout")
    @HystrixCommand
    public GlobalResult<String> timeout() {
        return paymentService.timeout();
    }

    public GlobalResult<String> fallback() {
        return GlobalResult.error(ResultCode.SYS_BUSY.getCode(),
                "Service Order System is busy, please try again later");
    }
}
